package net.lunjin.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.lunjin.bean.News;
import net.lunjin.service.NewsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import finaler.util.Page;
import finaler.util.PageList;

@Controller
@RequestMapping("tag")
public class TagController {
	
	@Autowired
	private NewsService newsService;
	/**
	 * 首页
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/" )
	public String index(HttpServletRequest request) throws Exception {
		Page page = new Page(request);
		page.setRange(100);//每页100个标签
		page.putParameter("newsType", "22");
		page.putParameter("sort", "hit desc");
		page.putParameter("searchkey", request.getParameter("searchkey"));
		PageList tagList = newsService.getNewsList(page);//标签查询
		request.setAttribute("tagList", tagList);
		return "/tag/index";
	}
	/**
	 * tag详细页
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping( value="/{tagId}.html" )
	public String tag(@PathVariable BigDecimal tagId,HttpServletRequest request) throws Exception {
		News tagDetail = newsService.findByKey(tagId);//标签查询
		request.setAttribute("tagDetail", tagDetail);

		List<News> tagList = newsService.getHotRecord(22, 50);//50条最热tag
		request.setAttribute("tagList", tagList);
		
		News updateNews = new News();
		updateNews.setId(tagId);
		updateNews.setHit(tagDetail.getHit( ).add(new BigDecimal(1)));
		newsService.update(updateNews);
		
		Page page = new Page(request);
		page.setRange(20);
		page.putParameter("newsType", "0 or newsType = 1 or newsType = 20 or newsType = 21");
		page.putParameter("searchkey", tagDetail.getNewstitle());
		PageList aboutNewsList = newsService.getNewsList(page);//相关新闻查询
		request.setAttribute("aboutNewsList", aboutNewsList);
		
		return "/tag/tag";
	}

}
